// sendGet('/sys/group/lower', {}).then(res => console.info(res)).catch(error => console.info(error))
layui.use(['table','form'], function(){
    var form = layui.form;
    var table = layui.table;

    //第一个实例
    var tableIns = table.render({
        elem: '#userList',
        url : 'http://127.0.0.1:8081/sntk/sys/user/page',
        cellMinWidth : 95,
        page : true,
        toolbar: '#toolbarpannel',
        height : "full-125",
        limits : [10,15,20,25],
        limit : 20,
        id : "userListTable",
        cols : [[
            {type: "checkbox", fixed:"left", width:50},
            {field: 'userId', title: 'ID', minWidth:50, align:"center"},
            {field: 'uname', title: '账户名', minWidth:70, align:"center"},
            {field: 'userName', title: '用户名', minWidth:100, align:"center"},
            {field: 'tel', title: '电话', minWidth:150, align:"center"},
            {field: 'email', title: '用户邮箱', minWidth:200, align:'center',templet:function(d){
                    if(d.email == null){
                        console.info('--------')
                        return ''
                    }else{
                        return '<a class="layui-blue" href="mailto:'+d.email+'">'+d.email+'</a>';
                    }

                }},
            {field: 'agencyCode', title: '机构',  align:'center',templet:function(d){
                    if(d.agencyDTO != null){
                        return d.agencyDTO.agencyName;
                    }
                    else{
                        return '';
                    }
                }},
            {field: 'groupId', title: '部门',  align:'center',templet:function(d){
                    if(d.departmentDTO != null){
                        return d.departmentDTO.groupName;
                    }
                    else{
                        return '';
                    }
                }},
            {field: 'createTime', title: '创建时间', align:'center',minWidth:150, templet: function (d) {
                    return dateFormat("yyyy-MM-dd HH:mm:ss",new Date(d.createTime))
                }},
            {field: 'loginTime', title: '最后登录时间', align:'center',minWidth:150}
        ]]
    });
    function getAgency(code){
        sendGet('/sntk/sys/agency/search/'+code, {})
            .then(res => {
                if(res.data.code == "200"){
                    $("#agency").empty()
                    $("#agency").append("<option value=''>请选择机构</option>");
                    let agencys = res.data.data.res
                    for(let index in agencys){
                        $("#agency").append("<option value='"+agencys[index].code+"'>"+agencys[index].agencyName+"</option>");
                    }
                    form.render('select'); //刷新select选择框渲染
                }
            })
            .catch(error => console.info(error))
    }
    getAgency('');
    //通过机构编号获取部门信息
    function getDepartment(code){
        if(code == ''){
            $("#department").empty()
            $("#department").append("<option value=''>请选择部门</option>");
            form.render('select'); //刷新select选择框渲染
            return
        }
        sendGet('/sntk/sys/group/search/'+code, {})
            .then(res => {
                if(res.data.code == "200"){
                    $("#department").empty()
                    $("#department").append("<option value=''>请选择部门</option>");
                    let departments = res.data.data.res
                    for(let index in departments){
                        $("#department").append("<option value='"+departments[index].groupId+"'>"+departments[index].groupName+"</option>");
                    }
                    form.render('select'); //刷新select选择框渲染
                }
            })
            .catch(error => console.info(error))
    }

    form.on('select(agencyfilter)', function(data){

        getDepartment(data.value)
    });

    //监听事件
    table.on('toolbar(btnfilter)', function(obj){
        //     var checkStatus = table.checkStatus(obj.config.id);
        switch(obj.event){
            case 'add':
                //添加用户
                var index = layui.layer.open({
                    title : "添加用户",
                    type : 2,
                    content : "./userAdd.html",
                    success : function(layero, index){
                        var body = layui.layer.getChildFrame('body', index);
                        if(edit){
                            body.find(".userName").val(edit.userName);  //登录名
                            body.find(".userEmail").val(edit.userEmail);  //邮箱
                            body.find(".userSex input[value="+edit.userSex+"]").prop("checked","checked");  //性别
                            body.find(".userGrade").val(edit.userGrade);  //会员等级
                            body.find(".userStatus").val(edit.userStatus);    //用户状态
                            body.find(".userDesc").text(edit.userDesc);    //用户简介
                            form.render();
                        }
                        setTimeout(function(){
                            layui.layer.tips('点击此处返回用户列表', '.layui-layer-setwin .layui-layer-close', {
                                tips: 3
                            });
                        },500)
                    }
                })
                layui.layer.full(index);
                window.sessionStorage.setItem("index",index);
                //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
                $(window).on("resize",function(){
                    layui.layer.full(window.sessionStorage.getItem("index"));
                })

                break;
            case 'delete':
                //         layer.msg('删除');
                break;
            case 'search':
                //查询
                let userName = $("#userName").val()
                let agencyCode = $("#agency").val()
                let groupId = $("#department").val()
                table.reload('userListTable', {
                    url: 'http://127.0.0.1:8081/sntk/sys/user/page'
                    ,where: {
                        userName: userName,
                        agencyCode: agencyCode,
                        groupId: groupId
                    } //设定异步数据接口的额外参数
                });
                break;
        };
    });

});